Skip to content

Conversation

@mgiannopoulos24
Copy link
Contributor

Description

This PR introduces a "Lock" feature for code snippets. When a snippet is locked, it becomes read-only in the editor and cannot be
trashed or permanently deleted until it is unlocked. This provides an essential safety layer for critical production snippets.

Related issue

Issue #256

Changes

  1. Database & Core Logic (PHP)
    • Schema: Added a locked (TINYINT) column to the snippets database tables in DB::create_table.
    • Data Model: Updated the Snippet class and Data_Item logic to support the new property.
    • Enforcement: Modified save_snippet, trash_snippet, and delete_snippet in snippet-ops.php to intercept and block modification or deletion requests if the snippet's lock status is active.
  2. REST API
    • Controller: Updated Snippets_REST_Controller to include locked in the JSON schema, allowing the React frontend to read and persist the lock state.
    • Fetching: Modified the DB fetch queries to ensure the locked property is returned in collection and single-item requests.
  3. Frontend Editor (React & TypeScript)
    • State Management: Updated useSnippetForm to automatically set the editor to isReadOnly mode when a snippet is locked.
    • Race Condition Fix: Enhanced useSubmitSnippet to accept a snippet override, ensuring that immediate toggles (like the Lock switch) send the most recent data to the server without waiting for a re-render.
    • Components:
      • Created LockControl.tsx for the sidebar.
      • Updated DeleteButton.tsx to disable itself when the lock is active.
        Integrated the toggle into the EditorSidebar.
  4. List Table UI
    • Visibility: Added a dedicated "Locked" column immediately following the snippet name.
    • Icons: Implemented a "Tick" (dashicons-yes) for locked items and a "Dimmed X" (dashicons-no-alt) for unlocked items.
    • Row Actions: Dynamically hidden the "Trash" action link in the list table for any snippet that is currently locked.

Screenshots

  • Showcase of locked column:

  • Showcase of the editor of a locked script:

How to Test

  1. Navigate to Snippets > Settings > Debug and click Upgrade Database Table.
  2. Edit an existing snippet and toggle the Lock Snippet switch in the sidebar to ON.
  3. Verify that the code editor and title field become uneditable.
  4. Verify that the Delete button in the sidebar is disabled.
  5. Return to the All Snippets list and verify the new Locked column shows a blue tick.
  6. Hover over the locked snippet in the list and confirm the Trash link is missing.
  7. Unlock the snippet and verify all fields and delete actions are restored.

@mgiannopoulos24
Copy link
Contributor Author

mgiannopoulos24 commented Dec 19, 2025

@imantsk Should I change the branch I make the PRs on, in future PRs? I saw yesterday that after getting merged to core branch, it got reverted.

@imantsk
Copy link
Member

imantsk commented Dec 22, 2025

Hi @mgiannopoulos24 👋
First of all, huge thank you for all your contributions! Youre doing some great work!
To address your question, yes i reverted the merge to the main branch core because we will release these changes in beta first.
Also for the same reason, id like to kindly ask you, if you could change the base branch to core-beta

@mgiannopoulos24
Copy link
Contributor Author

Okay sure thing. Thanks for letting me know

@mgiannopoulos24 mgiannopoulos24 changed the base branch from core to core-beta December 22, 2025 11:31
condition_id BIGINT(20) NOT NULL DEFAULT 0,
priority SMALLINT NOT NULL DEFAULT 10,
active TINYINT(1) NOT NULL DEFAULT 0,
locked TINYINT(1) NOT NULL DEFAULT 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it might cause unexpected side-effects to legacy users, we usually try to avoid altering the **_snippets table.
May i suggest you to consider wp_options instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it yes.

@mgiannopoulos24
Copy link
Contributor Author

I'm really sorry for making a mess on the commits. I merged the wrong branch and I had to revert this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants